package top.ezzz.config;


import cn.hutool.core.lang.Dict;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class AppException extends RuntimeException {

    private int code;

    private String msg;

    public static AppException operation() {
        AppException app = new AppException();
        app.setCode(1501);
        app.setMsg("操作异常");
        return app;
    }

    public static AppException operation(String msg) {
        AppException app = new AppException();
        app.setCode(1501);
        app.setMsg(msg);
        return app;
    }

    public static AppException business() {
        AppException app = new AppException();
        app.setCode(1502);
        app.setMsg("业务异常");
        return app;
    }

    public static AppException business(String msg) {
        AppException app = new AppException();
        app.setCode(1502);
        app.setMsg(msg);
        return app;
    }

    public static AppException exception(int code, String msg) {
        AppException app = new AppException();
        app.setCode(code);
        app.setMsg(msg);
        return app;
    }

    public static AppException convert(Throwable exception) {
        AppException app = new AppException();
        app.setCode(500);
        app.setMsg(exception.getMessage());
        return app;
    }

    public Dict toMap() {
        return new Dict()
                .set("code", this.code)
                .set("msg", this.msg);
    }


}
